<template>
	<view class="contentBox">
		<!-- <view class="big-title">问卷调查</view> -->
		<view>
			<view class="questionBox" v-for="(item,index) in jsonData" :key="index">
				<view>
					<view>{{index+1}}.{{item.title}}</view>
				</view>

				<view>
					<radio-group class="radioBox" @change="radioChange">
						<template v-for="(item2,index2) in item.askList">
							<radio :value="index+','+item2.content.substring(0,1)+','+index2" :key="index2"
								:checked="item2.checked" :label="item2.checked" color="#457cde">
								{{item2.content}}
							</radio>
						</template>
					</radio-group>
				</view>

				<!-- 评价 -->
				<!-- 	<view v-else-if="item.qd_answer_type==10" style="margin: 20rpx 10rpx;">
					<uni-easyinput type="textarea" v-model="item.qd_value" placeholder="请输入内容"></uni-easyinput>
				</view> -->
				<!-- 多选 -->
				<!-- <view v-else-if="item.qd_answer_type==30">
					<checkbox-group v-model="item.qd_value" class="radioBox">
						<checkbox v-for="info in item.qd_answer_options" :key="info.key" :label="info.key.toString()"
							color="#457cde">
							{{info.display_name}}
						</checkbox>
					</checkbox-group>
				</view> -->
				<!-- 单选 -->
				<!-- 	<view v-else-if="item.qd_answer_type==40">
					<radio-group class="radioBox" @change="radioChange">
						<template v-for="status in item.qd_answer_options">
							<radio v-model="item.qd_value" :key="status.key" :label="status.key.toString()"
								color="#457cde">
								{{status.display_name}}{{item.qd_value}}
							</radio>
						</template>
					</radio-group>
				</view> -->
			</view>
		</view>
		<view class="btn" @click="toHome" v-if="jsonData.length!==0">提交</view>
	</view>
</template>

<script>
	import {
		getAppQuestionaireInfo,
		addAppQuestionaireInfo
	} from '@/api/jz/questionnaire/info.js';
	import {
		getInfoId
	} from '@/utils/auth';
	export default {
		data() {
			return {
				current: 0,
				jsonData: [],
				data: [],
				numA: 0,
				numB: 0,
				numC: 0,
				numD: 0,
				courseId: 0
			};
		},
		onLoad(option) {
			this.courseId = option.courseId
			this.getList(option.courseId);
		},
		mounted() {

		},
		methods: {
			radioChange(evt) {
				let index = evt.detail.value.split(",")[0];
				for (var i = 0; i < this.jsonData[index].askList.length; i++) {
					if (i == evt.detail.value.split(",")[2]) {
						this.jsonData[index].askList[i].isTrue = evt.detail.value.split(",")[1];

					} else {
						this.jsonData[index].askList[i].isTrue = null;
					}
				}
			},
			toHome() {

				for (var i = 0; i < this.jsonData.length; i++) {
					for (var j = 0; j < this.jsonData[i].askList.length; j++) {
						if (this.jsonData[i].askList[j].isTrue) {
							this.data.push(this.jsonData[i].askList[j].isTrue);
							break;
						}
					}
				}
				this.getRecordNum();

				let data = {
					courseId: this.courseId,
					stuId: getInfoId(),
					numA: this.numA,
					numB: this.numB,
					numC: this.numC,
					numD: this.numD
				}
				if((this.numA+this.numB+this.numC+this.numD)!=this.jsonData.length){
					this.$modal.msgError("有未选中的,不能提交~！")
					return
				}
				addAppQuestionaireInfo(data).then(res => {
					if (res.code == 200) {
						this.$modal.msgSuccess(res.msg)
						// setTimeout(function() {
							uni.navigateBack({
								delta: 1
							});
						// }, 1000)

					}
				})
			},
			getRecordNum() {
				let index = this.data.indexOf('A')
				let indexb = this.data.indexOf('B')
				let indexc = this.data.indexOf('C')
				let indexd = this.data.indexOf('D')
				this.numA = 0
				this.numB = 0
				this.numC = 0
				this.numD = 0
				while (index !== -1) {
					this.numA++
					index = this.data.indexOf('A', index + 1)
				}

				while (indexb !== -1) {
					this.numB++
					indexb = this.data.indexOf('B', indexb + 1)
				}

				while (indexc !== -1) {
					this.numC++
					indexc = this.data.indexOf('C', indexc + 1)
				}

				while (indexd !== -1) {
					this.numD++
					indexd = this.data.indexOf('D', indexd + 1)
				}
			},
			getList(id) {
				var params = {
					pageNum: 1,
					pageSize: 99999,
          courseId:this.courseId
				}
				getAppQuestionaireInfo(params).then((res) => {
					this.jsonData = []
					if (res.code == 200) {
						this.jsonData = res.rows;
					}
				})
			}
		}
	};
</script>

<style lang="scss">
	.radioBox>>>uni-radio .uni-radio-input {
		margin: 0 20px !important;
	}

	.contentBox {
		// display: flex;
		flex-direction: column;
		// align-items: center;
		// justify-content: center;
		background-color: #fff;
	}

	.big-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #457cde;
		margin: 20rpx 0rpx 0rpx 0rpx;
	}

	.questionBox {
		padding: 0px 20rpx 20rpx 20rpx;
	}

	.qutitle {
		display: flex;
		font-size: 28rpx;
		background-color: #f2f2f2;
		padding: 10rpx 20rpx;
	}

	.comment {
		background-color: white;
		margin-top: -50rpx;
	}

	.radioBox {
		margin: 10rpx 20rpx;
		display: flex;
		flex-direction: column;
	}

	radio {
		font-size: 28rpx;
		margin: 10rpx 20px !important;
	}

	checkbox {
		font-size: 28rpx;
		margin: 10rpx 20px !important;
	}

	.topic {
		font-size: 32rpx;
		font-weight: bold;
		color: #457cde;
		padding: 10rpx 0rpx;
	}

	.para-text {
		font-size: 28rpx;
		font-weight: bold;
		color: #444444;
		padding: 10rpx 0rpx;
	}

	.btn {
		width: 50%;
		padding: 20rpx 30rpx;
		margin: 40rpx 25%;
		font-size: 28rpx;
		color: white;
		background-color: #3677d3;
		border-radius: 10rpx;
		letter-spacing: 4rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>
